import java.util.Arrays;
import java.util.Stack;

public class Sort {

    public static void insertSort(int[]array){
        for(int i=1;i<array.length;i++){
            int tmp=array[i];
            int j = i-1;
            for (; j >=0 ; j--) {
                if(array[j]>tmp){
                    array[j+1]=array[j];
                }else{
                    break;
                }
            }
            array[j+1]=tmp;
        }
    }

    public static void shellSort(int []array){
        int gap=array.length;
        while(gap>1){
            shell(array,gap);
            gap/=2;
        }
        shell(array,1);
    }
    public static void shell(int[]array,int gap){
        for (int i = 1; i <array.length ; i++) {
            int tmp=array[i];
            int j = i-gap;
            for (; j >=0 ; j-=gap) {
                if(array[j]>tmp){
                    array[j+gap]=array[j];
                }else{
                    break;
                }
            }
            array[j+gap]=tmp;
        }
    }

    public static void selectSort(int[]array){
        for (int i = 0; i < array.length; i++) {
            for (int j = i+1; j <array.length ; j++) {
                if(array[j]<array[i]){
                    int tmp=array[i];
                    array[i]=array[j];
                    array[j]=tmp;
                }
            }
        }
    }

    public static void heapSort(int[]array){
        createHeap(array);
        int end=array.length-1;
        while(end>0){
            int tmp=array[0];
            array[0]=array[end];
            array[end]=tmp;
            shiftDown(array,0,end);
            end--;
        }
    }
    public static void createHeap(int[]array){
        for (int parent= (array.length-1-1)/2; parent >=0 ; parent--) {
            shiftDown(array,parent,array.length-1);
        }
    }
    public static void shiftDown(int[]array,int parent,int len){
        int child=2*parent+1;
        while(child<len){
            if(child+1<len&&array[child+1]>array[child]){
                child++;
            }
            if(array[parent]<array[child]){
                int tmp=array[parent];
                array[parent]=array[child];
                array[child]=tmp;
                parent=child;
                child=2*parent+1;
            }else{
                break;
            }
        }
    }


    public static void quickSort(int []array){
        quick(array,0,array.length-1);
    }
    public static void quick(int []array,int start,int end){
        if(start>end){
            return;
        }
        int midValIndex=findMidValIndex(array,start,end);
        int tmp=array[start];
        array[start]=array[midValIndex];
        array[midValIndex]=tmp;

        int pivot=partition(array,start,end);
        quick(array,start,pivot-1);
        quick(array,pivot+1,end);
    }
    public static int findMidValIndex(int[]array,int start,int end){
        int mid=start+((end-start)>>>1);
        if(array[start]<array[end]){
            if(array[mid]<array[start]){
                return start;
            }else if(array[mid]>array[end]){
                return end;
            }else{
                return mid;
            }
        }else{
            if(array[mid]>array[start]){
                return start;
            }else if(array[mid]<array[end]){
                return end;
            }else{
                return mid;
            }
        }
    }

    public static int partition(int[]array,int start,int end){
        int tmp=array[start];
        while(start<end){
            while(start<end&&array[end]>=tmp){
                end--;
            }
            array[start]=array[end];
            while(start<end&&array[start]<=tmp){
                start++;
            }
            array[end]=array[start];
        }
        array[start]=tmp;
        return start;
    }

    public static void quickSort1(int[]array){
        Stack<Integer>stack=new Stack<>();
        int left=0;
        int right=array.length-1;
        int pivot=partition(array,left,right);
        if(pivot>left+1){
            stack.push(left);
            stack.push(pivot-1);
        }
        if(pivot<right-1){
            stack.push(pivot+1);
            stack.push(right);
        }
        while(!stack.isEmpty()){
            right=stack.pop();
            left=stack.pop();
            pivot=partition(array,left,right);
            if(pivot>left+1){
                stack.push(left);
                stack.push(pivot-1);
            }
            if(pivot<right-1){
                stack.push(pivot+1);
                stack.push(right);
            }
        }
    }

    public static void bubbleSort(int[]array){
        for (int i = 0; i < array.length-1; i++) {
            boolean flg=true;
            for (int j = 0; j < array.length-1-i; j++) {
                if(array[j]>array[j+1]){
                    int tmp=array[j+1];
                    array[j+1]=array[j];
                    array[j]=tmp;
                    flg=false;
                }
            }
            if(flg){
                break;
            }
        }
    }

    public static void main(String[] args) {
        int[]array={88,44,55,22,34,54,221,1};
        //insertSort(array);
        //shellSort(array);
        //selectSort(array);
        //heapSort(array);
        //bubbleSort(array);
        quickSort(array);
        System.out.println(Arrays.toString(array));
    }
}
